package Lesson;

//数组工具类
public class ClassPractice {
    public static void main(String[] args){
        int[] array={1,2,3,4,5,5,54,3,1};
        System.out.println(add(array,3));
        System.out.println(delete(array,2));
        System.out.println(sort(array));
        System.out.println(distinct(array));

    }
    /*
       添加元素到数组末尾
     */
    public static int[] add(int[] array,int num){
        int[] addArray=new int[array.length+1];
        for(int i=0;i<array.length;i++){
            addArray[i]=array[i];
        }
        addArray[array.length]=num;
        return addArray;
    }

    /*
       删除指定元素
     */
    public static int[] delete(int[] array,int index){
        int[] deleteArray=new int[array.length-1];
        for(int i=0;i<index;i++){
            deleteArray[i]=array[i];
        }
        for(int i=index;i<deleteArray.length;i++){
            deleteArray[i]=array[i+1];
        }
        return deleteArray;
    }

    /*
       给定数组，对数组元素进行排序
     */
    public static int[] sort(int[] array){
        for(int i=0;i<array.length-1;i++){
            for(int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
        return array;
    }

    /*
       给定数组，进行去重操作
     */
    public static int[] distinct(int[] array){
        int[] tempAry=new int[array.length];
        int index=0;
        for(int i=0;i<array.length;i++){
            boolean isRepeat=false;
            for(int j=0;j<index;j++){
                if(array[i]==tempAry[j]){
                    isRepeat=true;
                    break;
                }
            }
            if(!isRepeat){
                tempAry[index]=array[i];
                index++;
            }
        }
        int[] newAry=new int[index];
        for(int i=0;i<newAry.length;i++){
            newAry[i]=tempAry[i];
        }
        return newAry;
    }











}
